package map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * java.util.Map查找表
 */
public class MapDemo1 {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        Integer value = map.put("语文", 99);
        System.out.println(value);//遍历所有的key
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println("key:" + key);
        }
        //遍历所有的键值对
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> e : entrySet) {
            String key = e.getKey();
            Integer value1 = e.getValue();
            System.out.println(key + ":" + value1);
        }
        //遍历所有的值
        Collection<Integer> values = map.values();
        //新循环遍历
        for (Integer value2 : values) {
            System.out.println("value:" + value2);
        }
        //forEach遍历
        values.forEach(s-> System.out.println(s));
        map.put("语文", 99);
        map.put("数学", 98);
        map.put("英语", 78);
        map.put("物理", 67);
        map.put("化学", 87);
        System.out.println(map);

        Integer value1 = map.put("语文", 80);
        System.out.println(value1);
        System.out.println(map);

        value = map.get("数学");
        System.out.println("数学:" + value);
        value = map.get("体育");
        System.out.println("体育:" + value);

        value = map.remove("化学");
        System.out.println(value);
        System.out.println(map);

        int size = map.size();//获取map中的元素个数(一组键值对就是一个元素)
        System.out.println("size:" + size);

        boolean ck=map.containsKey("数学");
        System.out.println("包含key:"+ck);
        boolean cv=map.containsValue(96);
        System.out.println("包含value:"+cv);

        map.clear();//清空所有的元素
    }
}
